<?php

namespace App\Http\Controllers\Live;

use App\Http\Controllers\Controller;
use App\Models\Live\LiveGroupModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use MongoDB\Client;

class RoomController extends Controller
{
    public static function pullRoomListOnlineCount(){
        $roomList=DB::connection('shop')->table('live_room')->where([
            'status'=>2
        ])->get();
        $countList=[];
        foreach($roomList as $k=>$v){
            if(!empty($v->msg_group_id))
            $countList[$k]=[
                "room_id"=>$v->msg_group_id,
                "count"=>LiveGroupModel::getGroupOnlineUserCount($v->msg_group_id)
            ];
            $mongoUri=env("MONGO_URI");
            if(empty($mongoUri)){
                Log::debug(("mongouri is empty"));
                return;
            }
            $mongo=new Client($mongoUri);
            $db=$mongo->selectDatabase("zhideai_chatroom");
            $coll=$db->selectCollection("online_status_list");
            $v=LiveGroupModel::getGroupOnlineUserCount($v->msg_group_id);
            if($v!==false){
             $data=[
                "room_id"=>$v->msg_group_id,
                "count"=>$v,
                "last_time"=>time(),
                "last_time_str"=>date('Y-m-d H:i:s')];
             $obj=$coll->insertOne($data);
             $countList[]=$obj->getInsertedId();
            }
        }
        return $countList;
    }
}
